<?php
/**
 * Created by PhpStorm.
 * User: king
 * Date: 2019/3/2
 * Time: 14:45
 */

namespace app\index\controller;

use think\Db;
use think\Session;
use think\Controller;

class Reals extends Controller
{
//    实名认证
    public function shiming(){
    	  $res=db('user')
                ->join('f_real','f_user.uid=f_real.uid','left')
                ->join('f_package','f_real.tid=f_package.tid','left')
                ->where('f_real.rflg','neq',0)
                ->where('phone','neq','')
                ->field('f_user.uid,f_real.rid,avatars_img,phone,head_portrait,starttime,stoptime,Reason,uname,card,front_img,reverse_img,f_real.s_createtime,price,phase_time,rflg')
                ->order('f_real.s_createtime desc');
		if(input('sub'))
        {
            $datemin = input('datemin', '');//最小时间
            $datemax = input('datemax', '');//最大的吧
            if(isset($datemin) && $datemin != ''){
                $res = $res->whereTime('f_real.s_createtime', '>=', input('datemin').'00:00:00');
            }
            if(isset($datemax) && $datemax != ''){
                $res = $res->whereTime('f_real.s_createtime', '<=',input('datemax').'23:59:59');
            }
			$key=trim(input('Key'));//输入框
			if($key==""){
				$res=$res->paginate(6,false,['query'=>request()->param()]);
				$page=$res->render();
				return view('shiming',['res'=>$res,'page'=>$page]);
			}
			$cateid=input('cateid');//下拉菜单
            if($cateid==""){
                $res=$res->paginate(6,false,['query'=>request()->param()]);
				$page=$res->render();
				return view('shiming',['res'=>$res,'page'=>$page]);
			}
            //判断输入内容
            switch ($cateid){
                case 'uname':$date=['uname'=>$key];break;//用户名
                case 'f_user.uid':$date=['uid'=>$key];break;//用户id
                case 'phone':$date=['phone'=>strval($key)];break;//个人手机
                case 'f_real.rid':$date=['rid'=>$key];break;//认证id
            }
           if($cateid!="uname"){
           		 $res=$res->where($cateid,'like','%'.$key.'%')->paginate(5,false,['query'=>request()->param()]);
           }else{
           		 $res=$res->where($cateid,'like','%'.$key.'%')->whereor('nickname','like','%'.$key.'%')->paginate(5,false,['query'=>request()->param()]);
           }                                              
           $page=$res->render();
            return view('shiming',['res'=>$res,'page'=>$page]);
		}
		else
		{
			$res=$res->paginate(6);
		    $page=$res->render();
			return view('shiming',['res'=>$res,'page'=>$page]);
		}
    }
//实名认证的修改通过
	public function suero(){
		$id=input('id');
		$tid=db('real')->where('rid',$id)->value('tid');
		$time=db('package')->where('tid',$tid)->value('phase_time');
		$starttime=date("Y-m-d H:i:s");
		$stoptime=date('Y-m-d H:i:s',strtotime('+'.$time.' month'));
		$date=["rflg"=>'1','starttime'=>$starttime,'stoptime'=>$stoptime];
		$rs=db('real')->where('uid',$id)->update($date);
        $xname=db('user')->where('uid',$id)->value('xname');
        if($xname!=""){
        	$uname=db('user')->where('uid',$id)->update(['uname'=>$xname]);
        }
		if($rs && $uname){
			return array('code'=>1,'msg'=>'成功');
		}
        else{
			return array('code'=>0,'msg'=>'失败');		
		}
		
	}
//实名认证的修改失败
	public function erro(){
		if(request()->isAjax()){
			$date=["rflg"=>'2','Reason'=>input('txt')];
			$id=input('rid');
			if(db('real')->where('rid',$id)->update($date)){
				return array('code'=>1,'msg'=>'删除成功');
			}
		}
		else{
			$id=input('id');
		    return view('Reason',['rid'=>$id]);   
			
		}
		
	}
//实名认证查看详情
    public function details(){
		$id=input('id');
		$res=db('user')
		->join('f_real','f_user.uid=f_real.uid','left')
		->join('f_package','f_real.tid=f_package.tid','left')
		->where('rid',$id)
		->field('f_user.uid uid,phone,uname,sex,f_real.rid rid,front_img,reverse_img,front_img,starttime,stoptime,phase_time,price')
		->select();
		return view('detalis',['res'=>$res]);
	}
//套餐显示
	public function attest(){
		$res=db('package')->select();
		$arr=db('privilege')->select();
		  return view('attest',['res'=>$res,'arr'=>$arr]);
	}
//套餐删除
    public function tdel(){
		$tid=input('id');
		if(db('package')->where('tid',$tid)->delete())
			return array('code'=>1,'msg'=>'删除成功');
		else
			return array('code'=>0,'msg'=>'删除失败');
	}
//套餐添加
	public function tadd(){
	   if(request()->isAjax()){
		   $date=["phase_time"=>input('moms'),"price"=>intval(input('mon')),"state"=>input('txt')];
		   $db=db('package')->insert($date);
			if($db){
				return array('code'=>1,'msg'=>'成功');
			}
			else{
				return 0;
			}
	   }
	   else{
		 return view('tadd');   
	   }
	}
//特权添加
    public function cadd(){
	   if(request()->isAjax()){
		   $date=["privilege"=>input('pos'),"state"=>input('state')];
			if(db('privilege')->insert($date)){
				return array('code'=>1,'msg'=>'成功');
			}
			else{
				return 0;
			}
	   }
	   else{
		 return view('cadd');   
	   }
	}
//特权删除
    public function cdel(){
		$id=input('id');
		if(db('privilege')->where('id',$id)->delete())
			return array('code'=>1,'msg'=>'删除成功');
		else
			return array('code'=>0,'msg'=>'删除失败');
	}
//帮你踩
	public function bangcai(){
		$res=db('bangcai')
                ->join('f_user','f_bangcai.uid=f_user.uid')
                ->field('f_bangcai.*,f_user.uname,f_user.nickname,head_portrait,f_user.phone,f_user.avatars_img')
                ->order('f_bangcai.id desc');
		 $rs=db('bangcai')
                ->join('f_user','f_bangcai.uid=f_user.uid')
                ->count();
		if(input('sub'))
        {
            $datemin = input('datemin', '');//最小时间
            $datemax = input('datemax', '');//最大的吧
			if(isset($datemin) && $datemin != ''){
                $res = $res->whereTime('f_bangcai.createtime', '>=', input('datemin').' 00:00:00');
            }
            if(isset($datemax) && $datemax != ''){
                $res = $res->whereTime('f_bangcai.createtime', '<=', input('datemax').' 23:59:59');
            }
			$key=input('Key');//输入框
			if($key==""){
				$res=$res->paginate(9,false,['query'=>request()->param()]);
				$page=$res->render();
				return view('bangcai',['res'=>$res,'rs'=>$rs,'page'=>$page]);
			}
			$cateid=input('cateid');//下拉菜单  
          if($cateid==""){
			   $res=$res->paginate(6,false,['query'=>request()->param()]);
				$page=$res->render();
				return view('bangcai',['res'=>$res,'rs'=>$rs,'page'=>$page]);
          }
            //判断输入内容
            switch ($cateid){
                case 'uname':$date=['uname'=>$key];break;//用户名
                case 'phone':$date=['phone'=>strval($key)];break;//个人手机
                case 'f_bangcail.id':$date=['id'=>intval($key)];break;//认证id
            }          
          if($cateid!="uname"){
           		 $res=$res->where($cateid,'like','%'.$key.'%')->paginate(5,false,['query'=>request()->param()]);
           }else{
           		 $res=$res->where($cateid,'like','%'.$key.'%')->whereor('nickname','like','%'.$key.'%')->paginate(5,false,['query'=>request()->param()]);
          }
		    $page=$res->render();
            return view('bangcai',['res'=>$res,'rs'=>$rs,'page'=>$page]);
		}
        else{
            $res=$res->paginate(9);
		    $page=$res->render();
			return view('bangcai',['res'=>$res,'rs'=>$rs,'page'=>$page]);
        }
    }
//帮你踩改变未处理与为处理状态
    public function updateflg(){
		$id=input('id');
		$date=["flg"=>'1'];
		$rs=db('bangcai')->where('id',$id)->update($date);
		if($rs){
			return 1;
		}
        else{
			return 0;		
		}
	}
//帮你踩设置
	public function bncset(){
		if(request()->isAjax()){
			$date=["workdate"=>input('workdate'),"way"=>input('way'),"tel"=>input('tel')];
            $rs=db('bangcais')->where('id',1)->update($date);
			if($rs){
				return array('code'=>1,'msg'=>'成功');
			}
			else{
				return array('code'=>0,'msg'=>'失败');
			}
		}else{
           $res=db('bangcais')->find();
			return view('bncset',['res'=>$res]);
		}
        
    }
//委托采购
	public function caigou(){
		$res=db('caigou')
                ->join('f_user','f_caigou.uid=f_user.uid')
                ->field('f_caigou.*,f_user.uname,f_user.phone,f_user.nickname,head_portrait,f_user.avatars_img')
                 ->order('f_caigou.id desc');
		$rs=db('caigou')
                ->join('f_user','f_caigou.uid=f_user.uid')
                ->count();
        if (input('sub')){
			$key=input('Key');//输入框
			$cateid=input('cateid');//下拉菜单
            $datemin = input('datemin', '');//最小时间
            $datemax = input('datemax', '');//最大的吧
			//echo $datemin;
			//echo $datemax;
			if(isset($datemin) && $datemin != ''){
                $res = $res->whereTime('f_caigou.createtime', '>=', input('datemin').' 00:00:00');
				// print_r($res);die;
            }
            if(isset($datemax) && $datemax != ''){
                $res = $res->whereTime('f_caigou.createtime', '<=', input('datemax').' 23:59:59');
            }
           if($cateid==""){
            $res=$res->paginate(6,false,['query'=>request()->param()]);
		    $page=$res->render();
			return view('caigou',['res'=>$res,'rs'=>$rs,'page'=>$page]);
			}

			if($key==""){
				$res=$res->paginate(6,false,['query'=>request()->param()]);
		    $page=$res->render();
			return view('caigou',['res'=>$res,'rs'=>$rs,'page'=>$page]);
			}
			else
			{ 
				//判断输入内容
				switch ($cateid){
					case 'uname':$date=['uname'=>$key];break;//用户名
					case 'f_user.uid':$date=['uid'=>$key];break;//用户id
					case 'phone':$date=['phone'=>strval($key)];break;//个人手机
					case 'f_caigou.id':$date=['id'=>intval($key)];break;//认证id
				}
               
				if($cateid!='uname'){
           		 $res=$res->where($cateid,'like','%'.$key.'%')->paginate(5,false,['query'=>request()->param()]);
           }else{
           		 $res=$res->where($cateid,'like','%'.$key.'%')->whereor('nickname','like','%'.$key.'%')->paginate(5,false,['query'=>request()->param()]);
           }                                                
				$page=$res->render();
				return view('caigou',['res'=>$res,'rs'=>$rs,'page'=>$page]);
			}
        }
		else{
            $res=$res->paginate(6);
		    $page=$res->render();
			return view('caigou',['res'=>$res,'rs'=>$rs,'page'=>$page]);
        }
        
    }
	//委托采购改变未处理与为处理状态
    public function upcflg(){
		$id=input('id');
		$date=["flg"=>'1'];
		$rs=db('caigou')->where('id',$id)->update($date);
		if($rs){
			return 1;
		}
        else{
			return 0;		
		}
	}
	//委托采购设置
	public function wtcgset(){
		if(request()->isAjax()){
			$date=["workdate"=>input('workdate'),"way"=>input('way'),"tel"=>input('tel')];
            $rs=db('caigous')->where('id',1)->update($date);
			if($rs){
				return array('code'=>1,'msg'=>'成功');
			}
			else{
				return array('code'=>0,'msg'=>'失败');
			}
		}else{
             $res=db('caigous')->find();
			return view('wtcgset',['res'=>$res]);
		}
        
    }
	
}